package com.sqznkj.Controller;

import com.sqznkj.Service.AdminService;

import com.sqznkj.Service.comIntroService;
import com.sqznkj.entity.artcle;
import com.sqznkj.entity.comIntro;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
public class upload {
    /**
     * 记号,替换img标签
     */
    private static final String KUOHAO = "\\([a-zA-Z0-9]+\\.(jpg|gif|bmp|bnp|png|jpeg)\\)";
    private static final String SRC = "[a-zA-Z0-9]+\\.(jpg|gif|bmp|bnp|png|jpeg)";
    @Resource
    AdminService service;
    @Resource
    comIntroService comService;

    /**
     * 图片上传
     *
     * @param request
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public String FileUpload(HttpServletRequest request, Map<String, Object> map) throws Exception {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request)
                .getFiles("file");
        List<String> list = new ArrayList<String>();
        MultipartFile file = null;
        BufferedOutputStream stream = null;
        for (int i = 0; i < files.size(); ++i) {
            file = files.get(i);
            list.add(file.getOriginalFilename());
            if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    stream = new BufferedOutputStream(new FileOutputStream(
                            new File("/data/upload/" + file.getOriginalFilename())));
                    stream.write(bytes);
                    stream.close();

                } catch (Exception e) {
                    stream = null;
                    return "error";
                }
            } else {
                return "error ";
            }
        }

        map.put("fileNames", list);
        return "success";
    }

    /**
     * 上传文章信息
     *
     * @param a
     * @param map
     * @return
     */
    @RequestMapping(value = "/uploadArtcle", method = RequestMethod.POST)
    public String addArtcle(artcle a, Map<String, Object> map) {
        Matcher matcher = Pattern.compile(SRC).matcher(a.getContent());
        Matcher matcher2 = Pattern.compile(KUOHAO).matcher(a.getContent());
        List<String> list = new ArrayList<String>();
        while (matcher.find()) {
            list.add(matcher.group());
        }
        int i = 0;
        while (matcher2.find()) {
            a.setContent(a.getContent().replace(matcher2.group(), "<br><img src=\"Path/" + list.get(i) + "\"><br>"));
            i++;
        }
        a.setContent(a.getContent().replace("\r\n", "<br>"));
        service.add(a);
        return "index2";
    }

    @RequestMapping("/uploadText")
    public String up() {
        return "uptext";
    }

    /**
     * 上传纯文本
     *
     * @param b
     * @return
     */
    @RequestMapping("/text")
    public String text(artcle b) {
        b.setContent(b.getContent().replace("\r\n", "<br>"));
        service.add(b);
        return "index2";
    }

    @RequestMapping("/shanchu")
    public String shan(Map<String, Object> map) {

        List<artcle> artcles = service.getArtcle();
        map.put("artcles", artcles);
        return "deletePage";
    }
    @RequestMapping("/deletePage")
    public String deletePage1(HttpServletRequest request) {
        String t = request.getParameter("title");
        service.delete(t);
        return "deleteSuccess";
    }
    @RequestMapping(value = "/certificateUpload", method = RequestMethod.POST)
    public String certificateUpload(HttpServletRequest request, Map<String, Object> map) throws Exception {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request)
                .getFiles("file");
        List<String> list = new ArrayList<String>();
        MultipartFile file = null;
        BufferedOutputStream stream = null;
        for (int i = 0; i < files.size(); ++i) {
            file = files.get(i);
            list.add(file.getOriginalFilename());
            if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    stream = new BufferedOutputStream(new FileOutputStream(
                            new File("/data/upload/ci/" + file.getOriginalFilename())));
                    stream.write(bytes);
                    stream.close();

                } catch (Exception e) {
                    stream = null;
                    return "error";
                }
            } else {
                return "error ";
            }
        }
        map.put("fileNames", list);
        return "success2";
    }
    @RequestMapping("/uploadshouye1")
    public String shouye1upload(MultipartFile file){
        if (file.isEmpty()){
            return "error";
        }
        OutputStream outputStream = null;
        InputStream inputStream = null;
        try {
            outputStream = new FileOutputStream("/data/upload/banner/shouye1.jpg");
            inputStream = file.getInputStream();
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = inputStream.read(bytes,0,bytes.length))!=-1){
               outputStream.write(bytes,0,length);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "index3";
    }
    @RequestMapping("/uploadBanner")
    public String uploadBanner(HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        if (files.isEmpty()){
            return "error";
        }
        OutputStream outputStream = null;
        InputStream inputStream = null;
        for (int i = 0 ; i<files.size();i++){
            try {
                inputStream = files.get(i).getInputStream();
                outputStream = new FileOutputStream("D:/upload/banner/banner" + (i+1) + ".jpg");
                byte[] bytes = new byte[1024];
                int length = 0;
                while ((length = inputStream.read(bytes,0,bytes.length))!=-1){
                    outputStream.write(bytes,0,length);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    outputStream.close();
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "index3";
    }

    @RequestMapping("/uploadComIntro")
    public String uploadComIntro(String content) {
        if ("".equals(content)) {
            return "isNull";
        }
        String newContent = content.replace("\r\n", "<br>");
        comService.update(new comIntro(newContent));
        return "index3";
    }
}